package com.ajie.slimming.util;

import java.io.File;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Locale;

import android.text.TextUtils;

/**
 * 工具类
 * 
 * @author Jie Liu
 * 
 */
public class Util
{
    /** 热量系数 */
    private static final double coefficient = 0.6;
    
    /**
     * 创建文件夹
     * 
     * @param dir
     */
    public static void createFileDir(String dir)
    {
        File file = new File(dir);
        if (!file.exists())
        {
            file.mkdirs();
        }
    }
    
    /**
     * 根据当前时间返回格式化后的时间
     * 
     * @return
     */
    public static long currentTime()
    {
        Date date = new Date(System.currentTimeMillis());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss",
                Locale.getDefault());
        return Long.parseLong(sdf.format(date));
    }
    
    public static String printHexString(byte[] b)
    {
        String src = "";
        for (int i = 0; i < b.length; i++)
        {
            String hex = Integer.toHexString(b[i] & 0xFF);
            if (hex.length() == 1)
            {
                hex = '0' + hex;
            }
            src += hex.toLowerCase(Locale.getDefault()) + " ";
        }
        return src.substring(0, src.length() - 1);
    }
    
    /**
     * @param dateStr
     *            传入格式：20140913143613
     * @return 输出格式：2014-09-13 14:36:13
     */
    public static String realTimeAdapter(String dateStr)
    {
        try
        {
            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMddHHmmss",
                    Locale.getDefault());
            long currentMillis = sdf2.parse(dateStr).getTime();
            Date date = new Date(currentMillis);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
                    Locale.getDefault());
            String fmt = sdf.format(date);
            return fmt;
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return "";
    }
    
    /** 获取标准体重 */
    public static double getStandardWeight(String sex, String height)
    {
        int heightValue = Integer.valueOf(height);
        if (TextUtils.equals(sex, "男"))
        {
            return (heightValue - 100) * 0.9;
        }
        else
        {
            return (heightValue - 100) * 0.8;
        }
    }
    
    /** 获取减肥目标空间 */
    public static double getTarget(String sex, String height, String real)
    {
        int realValue = Integer.valueOf(real);
        return realValue - getStandardWeight(sex, height);
    }
    
    /** 获取单次消耗热量值卡数 */
    public static double getHeatCalorie(String strength, String time)
    {
        int strengthValue = Integer.parseInt(strength, 16);
        int timeValue = Integer.parseInt(time, 16);
        
        return coefficient * Math.pow(1.3, strengthValue) * timeValue;
    }
    
    /** 获取热量相当于的脂肪重量 */
    public static double getHeatWeight(double calorie)
    {
        return calorie / 7700;
    }
    
    /** 获取热量相当于的脂肪重量 */
    public static double getHeatWeight(String calorie)
    {
        return getHeatWeight(Double.parseDouble(calorie));
    }
}
